/*
 * 主菜单界面
 */

#include "gui.h"
#include <stdlib.h>

#define MENU_ITEM	5
#define SHOW_X	600
#define SHOW_Y	0
#define HIDE_X	780
#define HIDE_Y	0

static struct Panel *pn_play_panel;
static struct Button *pp_button[MENU_ITEM];
static struct Checker *ck_stick;
static struct Timer *tm_stick;
static struct Label *lb_title;

static int stick_status;	//0 - 不动, 1 - 显示, 2 - 隐藏
static int stick_delay = 10;

static char *menustr[MENU_ITEM] = {
	"New Game",
	"Retract",
	"Save",
	"Load",
	"Return"
};

static void ACALLBACK (*btcall[MENU_ITEM])();

//由于定时器需要一个idle，这里放到render里。
void ACALLBACK pn_play_panel_render(struct Panel *pn, void *userdata)
{
	if (ctrl_test_timer(tm_stick)) {
		if (stick_status == 1) {	//显示过程
			int nx, ny;
			panel_get_pos(pn_play_panel, &nx, &ny);
			if (nx > SHOW_X)
				panel_set_pos(pn_play_panel, nx - 15, SHOW_Y);
		} else if (stick_status == 2 && (!check_get_value(ck_stick))) {	//隐藏过程
			if (stick_delay > 0) {
				stick_delay--;
			} else {
				int nx, ny;
				panel_get_pos(pn_play_panel, &nx, &ny);
				if (nx < HIDE_X)
					panel_set_pos(pn_play_panel, nx + 15, SHOW_Y);
			}
		}
	}
	
	
	
	int px, py;
	panel_get_abs_pos(pn, &px, &py);
	
	int w, h;
	panel_get_size(pn, &w, &h);
	
	video_drawimg_x(gui_get_box_bg(), 0, 0, w, h, px, py);
	video_set_color(0, 0, 255);
	video_rectangle(px, py, px + w, py + h);
	
}

void ACALLBACK pn_play_panel_mouse_enter(struct Panel *pn, void *userdata)
{
	stick_status = 1;
}
void ACALLBACK bt_pn_enter(struct Button *pn, void *userdata)
{
	stick_status = 1;
}
void ACALLBACK pn_play_panel_mouse_leave(struct Panel *pn, void *userdata)
{
	if (!check_get_value(ck_stick))
		stick_status = 2;
	stick_delay = 40;
}
void ACALLBACK bt_pn_leave(struct Button *pn, void *userdata)
{
	if (!check_get_value(ck_stick))
		stick_status = 2;
	stick_delay = 40;
}

void ACALLBACK play_panel_bt_click(struct Button *bt, void *userdata)
{
	int i = (int)userdata;
	if (btcall[i]) {
		(*btcall[i])();
	}
}

void play_panel_init()
{
	int i;
	
	pn_play_panel = panel_new(NULL, HIDE_X, HIDE_Y, 199, 599);
	
	panel_user_render(pn_play_panel, pn_play_panel_render);
	panel_mouse_enter(pn_play_panel, pn_play_panel_mouse_enter);
	panel_mouse_leave(pn_play_panel, pn_play_panel_mouse_leave);
	
	ck_stick = check_new(pn_play_panel, "stick", 20, 20, 25, 25);
	tm_stick = ctrl_create_timer();
	ctrl_set_timer(tm_stick, 50, 1);
	
	lb_title = label_new(NULL, "Cbar-five", 60, 40, 0, 0);
	label_set_font(lb_title, gui_get_font_big());
	
	for(i = 0; i < MENU_ITEM; ++i) {
		pp_button[i] = button_new(pn_play_panel, 45, i * 50 + 80, 120, 30);
		button_set_caption(pp_button[i], menustr[i]);
		button_clicked(pp_button[i], play_panel_bt_click);
		button_set_userdata(pp_button[i], (void *) i);
		button_enter(pp_button[i], bt_pn_enter);
		button_leave(pp_button[i], bt_pn_leave);
	}
	panel_set_visual(pn_play_panel, 0);
	panel_set_enable(pn_play_panel, 0);
}

void play_panel_set_callback(void (*new_g)(),
			     void (*retract)(),
			     void (*save)(),
			     void (*load)(),
			     void (*ret)())
{
	btcall[0] = new_g;
	btcall[1] = retract;
	btcall[2] = save;
	btcall[3] = load;
	btcall[4] = ret;
}

void play_panel_show()
{
	if (panel_get_visual(pn_play_panel) == 0) {
		panel_set_visual(pn_play_panel, 1);
		panel_set_enable(pn_play_panel, 1);
	}
	stick_status = 1;
}
